package clientPackage;
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import gui.AcademicFrame;
import gui.Dialog;
import gui.SendMailSSL;
import gui.ServerGui;
import gui.ServerLog;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import common.CommonMethods;

import Entities.AssessmentEntity;
import Entities.EmailEntity;
import Entities.ExtendEntity;
import Entities.InspectionEntity;
import Entities.RequestEntity;
import Entities.UserEntity;

import logic.MySQLconnection;



import ocsf.server.*;

/**
 * @author Adam Daxa, Guy Franco, Chen Parodi, Idan Mann
 * This class initializes the server instance and will handle communications between the 
 * clients and the database.
 * 
 * The server will receive ArrayList<Objects>s where the first index holds a String code
 * for the command involved.
 * The string would then be passed through an if -else chain to convert the string to and integer code
 * to use in a switch case, handling each command. 
 * (Eclipse Indigo does not support switch on a String variable)
 */
public class ObservableServer extends AbstractServer 
{
  //Class variables *************************************************
 
	private Connection conn;
	private MySQLconnection functions;
	final public static int DEFAULT_PORT = 5555;
	private ServerLog logger;
  
  //Constructors ****************************************************
  
  public ObservableServer(int port, String sqlServer, String sqlUsername, String sqlPassword, ServerLog myLog) 
  {
    super(port);
    logger=myLog;
    this.functions = new MySQLconnection(sqlServer, sqlUsername, sqlPassword);
    try 
 	{
         Class.forName("com.mysql.jdbc.Driver").newInstance();
     } catch (Exception ex) {/* handle the error*/} 	
	}
	

  
  //Instance methods ************************************************
  
  /**
   * This method handles any messages received from the client.
   *
   * @param msg The message received from the client.
   * @param client The connection from which the message originated.
   */
  public void handleMessageFromClient (Object msg, ConnectionToClient client)
  {
	  int i=0;
	  int scanCode=0;
	 ArrayList<Object> answer = new ArrayList<Object>();
	  
	  String command = (String)( ((ArrayList<Object>)msg).get(0) );
	  	  
	 scanCode = convertToScanCode( command );
	 
	 String logLine = new String (client.getName() + " " + command + " "); 
	 logger.addString(logLine);
	
	  switch (scanCode)
	  {
	  
	  case(001):
		  {
			try {
					
						answer = functions.login( (UserEntity) ((ArrayList<Object>)msg).get(1) );
					
						client.sendToClient(answer);

					}catch (IOException e){
					// TODO Auto-generated catch block
				//	e.printStackTrace();
				}
				break;
				
		  }
		  
	  case(002):
		  {
			
	
			  String userID = new String((String) ((ArrayList<Object>)msg).get(1));
			  answer = functions.getInfo(userID);
			  try {
				client.sendToClient(answer);

			} catch (IOException e) {
				// TODO Auto-generated catch block
			//	e.printStackTrace();
			}
				break;
		  }
		  
	  case(003):
	  {
	  	RequestEntity request=  (RequestEntity) ((ArrayList<Object>)msg).get(1);
	  	//int fileLength= (int) ((ArrayList<Object>)msg).get(3);
	  	int fileLength=  Integer.parseInt((((ArrayList<Object>)msg).get(3).toString()));
	  	byte[] bytes= (byte[]) ((ArrayList<Object>)msg).get(2);		
		answer = functions.request( (RequestEntity) ((ArrayList<Object>)msg).get(1) );
	  	if(fileLength>0){
	  		functions.uploadFile(request, bytes, fileLength);
	  	}
		try {
			client.sendToClient(answer);

		} catch (IOException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		}
		break;
	  }
		  
	  case(004):
		  {
			  
		  	answer = functions.getListOfColumn("systeminfo", "Description", "listOfSystems"); // General reusable method
			  try {
				client.sendToClient(answer);
			} catch (IOException e) 
			{
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}
				break;
		  }
		

	  case(007):
	  {
		
		  String userID = new String((String) ((ArrayList<Object>)msg).get(1));
		  answer = functions.getRequestHistory(userID);
		  try {
			client.sendToClient(answer);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
			break;
	  }
	  
	  case(8):
	  {
		
		  String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
		  answer = functions.getRequest(requestID);
		  try {
			client.sendToClient(answer);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
			break;
	  }
	  case(9):
	  {
		  
		
		  answer = functions.getReq((String)( ((ArrayList<Object>)msg).get(1)));
		  try {
			client.sendToClient(answer);
			} catch (IOException e) {
				// TODO Auto-generated catch block
			//	e.printStackTrace();
			}
				break;
		  }
	  case(102):
	  {
		
		  
		
		  String userID = new String((String) ((ArrayList<Object>)msg).get(1));
		  String currentStatus= new String((String) ((ArrayList<Object>)msg).get(2));
		  
		  
		  answer = functions.getlistOfRequests(userID,currentStatus);
		  try {
			  
			  
			client.sendToClient(answer);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		}
			break;
	  }
  case(101):
  {
	  scanCode=000;
	  String request = new String((String) ((ArrayList<Object>)msg).get(1));
	  String days = new String((String) ((ArrayList<Object>)msg).get(2));
	  String newStatus= new String((String) ((ArrayList<Object>)msg).get(3));
	  answer = functions.setTaskDuration(request,days,newStatus);
	  try { 
		client.sendToClient(answer);
	} catch (IOException e) {
		// TODO Auto-generated catch block
	//	e.printStackTrace();
	}
		break;
  }

    
    case(302):
    {  
		
      	String assID = new String((String) ((ArrayList<Object>)msg).get(1));	
      	answer = functions.getReviewAndDecision(assID); 
 
  		  try {
  			client.sendToClient(answer);
  		} catch (IOException e)
  		{
  		//	e.printStackTrace();
  		}
  			break;
    }
    
    case(303):
    {  
    	String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
      	String discussionLog = new String((String) ((ArrayList<Object>)msg).get(2));
      	answer = functions.setValueByRequestID("reviewanddecision", "reviewAndDecision", "reqID",  requestID,  discussionLog, "discussionLogAnswer" ); 
      	
  		  try {
  			client.sendToClient(answer);
  		} catch (IOException e)
  		{
  			//e.printStackTrace();
  		}
  			break;
    }
    
    case(304):
	  {
		
		  String assessmentID = new String((String) ((ArrayList<Object>)msg).get(1));
		  answer = functions.getAssessment(assessmentID);
		  try {
			client.sendToClient(answer);

		} catch (IOException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		}
			break;
	  }
  
  case(200):
  {
	  String userID = new String((String) ((ArrayList<Object>)msg).get(1));
	  String currentStatus= new String((String) ((ArrayList<Object>)msg).get(2));
	  String Elid = (String) ((ArrayList<Object>)msg).get(3);
	  String table = (String) ((ArrayList<Object>)msg).get(4);
	  answer = functions.getlistOfExRequests(userID,currentStatus,Elid,table);
	  try {
		client.sendToClient(answer);
	} catch (IOException e) {
		//e.printStackTrace();
	}
		break;
  }
  
  case(305):
  {  
    	
    	answer = functions.getRequestsWithTwoValues("request", "Status", "Rid", "5", "6", "reviewableRequests"); 
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
  }
  
  case(307):
  {  
	  	String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
	  	String discussionLog = new String((String) ((ArrayList<Object>)msg).get(2));
    	answer = functions.insertValuesToReviewAndDecision (requestID, discussionLog ); 
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
  }
  
  case(308):
  {  
	  	String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
    	answer = functions.convertRequestIDtoAssessmentID(requestID); 
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
  }
  
  case(309):
	  {
		
		  String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
		  answer = functions.getRequest(requestID);
		  try {
			client.sendToClient(answer);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
			break;
	  }
  case(310):
  {
	
	
	  answer = functions.getAss();
	  try {
		client.sendToClient(answer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
			break;
	  }
	  

  case(201):
  {
	  String code = ((String)((ArrayList<Object>)msg).get(0));
	  String request = new String((String) ((ArrayList<Object>)msg).get(1));
	  String det= new String((String) ((ArrayList<Object>)msg).get(2));
	  String newStatus = (String) ((ArrayList<Object>)msg).get(4);
	  functions.changeStatus(request, newStatus);
	  int req = Integer.parseInt(request);
	  answer = functions.getExeccutionEntity(code,req);
	  if (answer.get(1).equals("Valid"))
	  {
		  answer = functions.updateExecutedReq(code, request, det);
	  }
	  else{
		 answer = functions.enterNewExecutionRecord(code, request, det, "no");
	  }
	  if(!((ArrayList<Object>)msg).get(5).equals("nothing"))
	  {
		  ArrayList<Object> ex = new ArrayList<Object>();
		  String code1=((String)((ArrayList<Object>)msg).get(5));
		  int number = Integer.parseInt((((ArrayList<Object>)msg).get(6)).toString());
		  RequestEntity en = ((RequestEntity)((ArrayList<Object>)msg).get(7));
		  String status = ((String)((ArrayList<Object>)msg).get(8));
		  String pid1 = ((String)((ArrayList<Object>)msg).get(9));
		  ex = functions.fillInExceeded(code1,en.getRID(),en.getUid(),number,status);
		  ArrayList<Object> eleader = functions.getUsers("getEleader",pid1);
		  if(eleader.get(1).equals("Valid"))
		  {
			  EmailEntity eleMail = (EmailEntity) eleader.get(2);
			  String mail = eleMail.getMail();
			  SendMailSSL.SendEmail(mail, "Ass' exceeded", "For your notice ! the ass number" + request + "have Exceeded!"  );
			  SendMailSSL.SendEmail("braudeSupervisor@gmail.com","Ass' exceeded", "For your notice ! the ass number"+request + "have Exceeded!");
			  SendMailSSL.SendEmail("no1984name@hotmail.com","Ass' exceeded", "For your notice ! the ass number" + request + "have Exceeded!");
		  }
		  
		  
		  answer.add(ex);
	  }
	  else	// was put in order not to check out of bounds place .
	  {
		  ArrayList<Object> ex = new ArrayList<Object>();
		  String code1 = "fillInExceeded";
		  String Valid = "notValid";
		  ex.add(code1);
		  ex.add(Valid);
		  answer.add(ex);
	  }
	  try {
		client.sendToClient(answer);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
		break; 
  }
  
  case(900):
  {  
	  	ExtendEntity deadlineRequest = ((ExtendEntity) ((ArrayList<Object>)msg).get(1));
    	answer = functions.extendDeadlineRequest(deadlineRequest); 
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
  }
	  
  case(901):
  {  
	  	
    	answer.add("extentionRequestWasSent");
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  }
  
  case(902):
  {  
	  String requestID = ((String)((ArrayList<Object>)msg).get(1));
	  	answer = functions.changeStatus(requestID,"62");
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
  }
  

  case(903):
  {  
	  String requestID = ((String)((ArrayList<Object>)msg).get(1));
	  	answer = functions.changeStatus(requestID,"7");
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  }
  
  case(904):
  {  
	  String requestID = ((String)((ArrayList<Object>)msg).get(1));
	  String additionalInformation = ((String)((ArrayList<Object>)msg).get(2));
	  
	  	answer = functions.requireAdditionalInformation(requestID,additionalInformation);
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  }
  

  case(800):
  {

	
	  String userID = new String((String) ((ArrayList<Object>)msg).get(1));
	  answer = functions.getUser(userID);
	  try {
		client.sendToClient(answer);

	} catch (IOException e) {
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
		break;
  }
  case(801):
  {
	
	  String reqID = new String((String) ((ArrayList<Object>)msg).get(1));
	  String ID = new String((String) ((ArrayList<Object>)msg).get(2));
	  String identity =new String((String) ((ArrayList<Object>)msg).get(3));
	  String status =new String((String) ((ArrayList<Object>)msg).get(4));
	  
	  answer = functions.Pair(reqID,ID,identity,status);
	  try {
		client.sendToClient(answer);

	} catch (IOException e) {
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
		break;
  }
  case(802):
  {
	  
	
	  answer = functions.getExe();
	  try {
		client.sendToClient(answer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
			break;
	  }
	
  case(103):
  {
	 
	  AssessmentEntity assessmentEntity=((AssessmentEntity) ((ArrayList<Object>)msg).get(1));
	  String newStatus= new String((String) ((ArrayList<Object>)msg).get(2));
	  String requestID= assessmentEntity.getRid();
	  String userID = functions.requestToUserID(requestID);
	  String email;
	  
	  ArrayList<Object> requestArray = functions.getRequest(requestID);
	  RequestEntity request = (RequestEntity)(requestArray.get(1));
	  if (CommonMethods.exceededDeadline(request.getDeadline()))
	  {
		  functions.fillInExceeded("fillInExceeded", Integer.parseInt(requestID), request.getUid(),CommonMethods.getExceededTime(request.getDeadline()), request.getStatusid());
		  if (!userID.equalsIgnoreCase("none"))
		  {
			  ArrayList<Object> user = functions.getInfo(userID);
			  email = (String)user.get(3);
			SendMailSSL.SendEmail(email, "Deadline exceeded!", "You have exceeded the deadline for request " + requestID);
		  }
		
		
	  }
	  
	  
	  answer = functions.setAssessment(assessmentEntity, newStatus);
	  try {
		  
		  client.sendToClient(answer);
		  
	  } catch (IOException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
	  }
	  break;
  }

  case(202):
  {  
	  	ArrayList<Object> extra = new ArrayList<Object>();
	  	String code = new String((String) ((ArrayList<Object>)msg).get(0));
  		String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
		answer = functions.getRequest(requestID);
    	int request = Integer.parseInt(requestID);
    	extra = functions.getExeccutionEntity(code,request);
    	answer.add(extra);
    	try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
  }
  
  case(905):
  {  
	  String status = new String((String) ((ArrayList<Object>)msg).get(1));
	   	answer = functions.getListsForAssignment(status);
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  }
  
  case(906):
  {  
	  String userSkill = new String((String) ((ArrayList<Object>)msg).get(1));
	   	answer = functions.getListOfUsers(userSkill);
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  }
  case(803):
  {
	
	  String reqID = new String((String) ((ArrayList<Object>)msg).get(1));
	  String status = new String((String) ((ArrayList<Object>)msg).get(2));
	  answer = functions.approveEXDu(reqID, status);
	  try {
		client.sendToClient(answer);

	} catch (IOException e) {
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
		break;
  }
  case(804):
  {
	
	  String reqID = new String((String) ((ArrayList<Object>)msg).get(1));
		

	  answer = functions.getDu(reqID);
	  try {
		client.sendToClient(answer);

	} catch (IOException e) {
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
		break;
  }
  
  case(907):
  {  
	  String inspectorID = new String((String) ((ArrayList<Object>)msg).get(1));
	  String requestID = new String((String) ((ArrayList<Object>)msg).get(2));
	   	answer = functions.setInspectorToRequest(requestID, inspectorID);
		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  }
  case(805):
  {  
		

	  String reqID = new String((String) ((ArrayList<Object>)msg).get(1));
	  //int newDuration=(int)((ArrayList<Object>) msg).get(2);
	  int newDuration= Integer.parseInt((((ArrayList<Object>) msg).get(2)).toString());
		
	   	answer = functions.setNewDuration(reqID,newDuration);

		  try {
			client.sendToClient(answer);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  }
  case(205):
  {
	  String code = ((String)((ArrayList<Object>)msg).get(0));
	  String request = new String((String) ((ArrayList<Object>)msg).get(1));
	  String det= new String((String) ((ArrayList<Object>)msg).get(2));
	  String bool = new String((String) ((ArrayList<Object>)msg).get(3));
	  answer = functions.enterNewExecutionRecord(code,request,det,bool);
	  try {
			client.sendToClient(answer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		}
			break; 
  }
  case(204):
  {
	  String code = ((String)((ArrayList<Object>)msg).get(0));
	  String request = new String((String) ((ArrayList<Object>)msg).get(1));
	  String det= new String((String) ((ArrayList<Object>)msg).get(2));
	  answer = functions.updateExecutedReq(code,request,det);
	  try {
		client.sendToClient(answer);
	} catch (IOException e) {
	//	e.printStackTrace();
	}
		break; 
  }
  
  case(203):
  {  
	  	ArrayList<Object> extra = new ArrayList<Object>();
    	String code = ((String) ((ArrayList<Object>)msg).get(0));
    	String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
    	int request = Integer.parseInt(requestID);
    	extra = functions.getExeccutionEntity(code,request);
    	try {
			client.sendToClient(extra);
		} catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
  } 

case(908):
{  
		String inspectorID = new String((String) ((ArrayList<Object>)msg).get(1));
	   	answer = functions.getRequestsWithTwoValues("request", "Status", "Rid", "12", "5555", "listOfInspectionableRequests");
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
}



case(909):
{  
		String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
	   	answer = functions.getExeccutionEntity("executionEntity",Integer.parseInt(requestID));

		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
}

case(910):
{  
		String requestID = new String((String) ((ArrayList<Object>)msg).get(1));
	   	answer = functions.getInspection(requestID);
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
			
}

case(911):
{  
	
		InspectionEntity inspection = ((InspectionEntity) ((ArrayList<Object>)msg).get(1));
		ArrayList<Object> ans1 = new ArrayList<Object>();
		String action = ((String)((ArrayList<Object>)msg).get(2));
		answer = functions.setInspection(inspection, action);
	   
		if (action.equalsIgnoreCase("approve") && ((String)answer.get(1)).equalsIgnoreCase("Valid") )
	   	{
	   		ans1=functions.changeStatus(inspection.getRequestID(), "121");
	   	}
	   	else
	   	if (action.equalsIgnoreCase("deny") && ((String)answer.get(1)).equalsIgnoreCase("Valid") )
		{
		 	ans1=functions.changeStatus(inspection.getRequestID(), "122");
		}	
	   	else
	   	{
			ans1.add("somthing");
			ans1.add("StatusNotSet");
		}	
		answer.add(ans1.get(1).toString());
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
			
} 
case(806):
{  
	
	   	answer = functions.getExtend();
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
			
}

case(500):
{  
		
		answer = functions.initializeMangeEmployees();
		
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
			
} 
case(807):
{  
		String reqID = new String( (String)((ArrayList<Object>)msg).get(1));
	   	answer = functions.getExtendInfo(reqID);
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
			
}

case(501):
{  
		UserEntity oldChairman = ((UserEntity) ((ArrayList<Object>)msg).get(1));
		UserEntity newChairman = ((UserEntity) ((ArrayList<Object>)msg).get(2));
		answer = functions.switchChairman(oldChairman, newChairman);
	     
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
			
} 
case(104):
{
	 
	  AssessmentEntity assessmentEntity=((AssessmentEntity) ((ArrayList<Object>)msg).get(1));
	  answer = functions.newAssessmentRow(assessmentEntity);
	  try {
		  
		  client.sendToClient(answer);
		  
	  } catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
	  }
	  break;
}
case(808):
{  
		String code = new String((String) ((ArrayList<Object>)msg).get(1));
		String extID = new String((String) ((ArrayList<Object>)msg).get(2));
	   	answer = functions.approvDenyExt(code,extID);
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
	//		e.printStackTrace();
		}
			break;
			
}

case(502):
{  
		UserEntity oldCommitteeMember = ((UserEntity) ((ArrayList<Object>)msg).get(1));
		UserEntity newCommitteeMember = ((UserEntity) ((ArrayList<Object>)msg).get(2));
		answer = functions.switchCommitteeMembers(oldCommitteeMember, newCommitteeMember);
	     
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
			//e.printStackTrace();
		}
			break;
			
} 

case(206):
{
	
	String code = ((String) ((ArrayList<Object>)msg).get(0));
	String pid = ((String) ((ArrayList<Object>)msg).get(1));
	String oldStatus =((String) ((ArrayList<Object>)msg).get(2));
	answer = functions.getDeniedExecutions(code,pid,oldStatus);
	try {
		client.sendToClient(answer);
	}
	catch (Exception e)
	{
		e.getCause();
		
	}
	break;
}

case(207):
{
	ArrayList<Object> extra = new ArrayList<Object>();
	String code = ((String) ((ArrayList<Object>)msg).get(0));
	String rid = ((String) ((ArrayList<Object>)msg).get(1));
	String newStatus =((String) ((ArrayList<Object>)msg).get(2));
	answer = functions.changeStatus(rid,newStatus);
	extra = functions.DelExeReq(rid);
	try{
		client.sendToClient(extra);
	}
	catch(Exception e)
	{
		e.getCause();
		
	}
	break;
}

case(503):
{  
		UserEntity currentUser = ((UserEntity) ((ArrayList<Object>)msg).get(1));
		answer = functions.setUserAccess(currentUser);
	     
		  try
		  {
			client.sendToClient(answer);
		}catch (IOException e)
		{
		//	e.printStackTrace();
		}
			break;
			
}
case(105):
{
	answer = functions.changeStatus();
	try
	{
		client.sendToClient(answer);
	}
	catch (IOException e)
	{
	//	e.printStackTrace();
	}
	break;
}
case(809):
{
	String reqID = ((String) ((ArrayList<Object>)msg).get(1));
	String currentS = ((String) ((ArrayList<Object>)msg).get(2));
	String change = ((String) ((ArrayList<Object>)msg).get(3));
	String explanation =((String) ((ArrayList<Object>)msg).get(4));
	String supervisorID = ((String) ((ArrayList<Object>)msg).get(5));
	answer = functions.updateFields(reqID,currentS,change,explanation,supervisorID);
	try {
		client.sendToClient(answer);
		
	}
	catch (Exception e)
	{
		e.getCause();
		}
	break;
}
case(810):
{
	String status1 = ((String) ((ArrayList<Object>)msg).get(1));
	String status2 = ((String) ((ArrayList<Object>)msg).get(2));
	answer = functions.completeReq(status1,status2);
	try {
		client.sendToClient(answer);
		
	}
	catch (Exception e)
	{
		e.getCause();
		}
	break;
}



case(504):
{  
		String userID = ((String) ((ArrayList<Object>)msg).get(1));
		functions.logOff(userID);
		
	
			break;
}





case(811):
{
	String reqID = ((String) ((ArrayList<Object>)msg).get(1));
	String status = ((String) ((ArrayList<Object>)msg).get(2));
	answer = functions.changeStatus(reqID,status);
	try {
		client.sendToClient(answer);
		
	}
	catch (Exception e)
	{
		e.getCause();
		}
	break;
}
case(106):
{
	String reqID = ((String) ((ArrayList<Object>)msg).get(1));
	answer = functions.getFile(reqID);

	try{
		client.sendToClient(answer);
	}
	catch(IOException e)
	{
		//e.printStackTrace();
	}
	break;
}


case(812):
{
	String exeID = ((String) ((ArrayList<Object>)msg).get(1));
	
	answer = functions.getExecute(exeID);
	try {
		client.sendToClient(answer);
		
	}
	catch (Exception e)
	{
		e.getCause();
		}
	break;
}
case(813):
{
	String rID = ((String) ((ArrayList<Object>)msg).get(1));
	
	answer = functions.ridToAid(rID);
	try {
		client.sendToClient(answer);
		
	}
	catch (Exception e)
	{
		e.getCause();
		}
	break;
}

case(912):
{
	  
	answer = functions.getListOfColumn("request", "Rid", "getListOfAllRequests"); // General reusable method
	  try {
		client.sendToClient(answer);
	} catch (IOException e) 
	{
		// TODO Auto-generated catch block
	//	e.printStackTrace();
	}
		break;
}
case(913):
{
	  
	answer = functions.getListOfColumn("assessment", "Assid", "getListOfAllAssessments"); // General reusable method
	  try {
		client.sendToClient(answer);
	} catch (IOException e) 
	{
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
		break;
}


case(914):
{
	  
	answer = functions.getListOfColumn("assessment", "Assid", "getListOfAllExecutions"); // General reusable method
	  try {
		client.sendToClient(answer);
	} catch (IOException e) 
	{
		// TODO Auto-generated catch block
	//	e.printStackTrace();
	}
		break;
}


case(915):
{
	int numberofdays;
	String days;
	String requestID = ((String) ((ArrayList<Object>)msg).get(1));
	ArrayList<Object> request = functions.getRequest(requestID);
	
	RequestEntity myrequest = ((RequestEntity) ((ArrayList<Object>)request).get(1));
		
	numberofdays = functions.getexceedTime(requestID);
	numberofdays = numberofdays + CommonMethods.numberOfDays(myrequest.getDate(), myrequest.getDeadline());
	days= Integer.toString(numberofdays);
	
	answer = functions.changeStatus(requestID , "999");
	functions.setTaskDuration(requestID, days, "999");
	  try {
		client.sendToClient(answer);
	} catch (IOException e) 
	{
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
		break;
}

case(814):
{
         
          answer = functions.getAllReq();
          try {
                client.sendToClient(answer);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                        break;
          }
case(815):
{
        String rID = ((String) ((ArrayList<Object>)msg).get(1));
        String reason= ((String) ((ArrayList<Object>)msg).get(2));
        answer = functions.suspend(rID, reason);
        try {
                client.sendToClient(answer);
               
        }
        catch (Exception e)
        {
                e.getCause();
                }
        break;
}
case(816):
{
        String rID = ((String) ((ArrayList<Object>)msg).get(1));
        answer = functions.resume(rID);
        try {
                client.sendToClient(answer);
               
        }
        catch (Exception e)
        {
                e.getCause();
                }
        break;
}
case(817):
{
        String rID = ((String) ((ArrayList<Object>)msg).get(1));
        answer = functions.susInfo(rID);
        try {
                client.sendToClient(answer);
               
        }
        catch (Exception e)
        {
                e.getCause();
                }
        break;
}
case(107):
{
	String from = ((String) ((ArrayList<Object>)msg).get(1));
	String to = ((String) ((ArrayList<Object>)msg).get(2));
	answer = functions.activityReport(from, to);
	System.out.println("after answer sent to user");
	try {
		client.sendToClient(answer);		
	}
	catch (Exception e)
	{
		e.getCause();
	}
	break;
}
case(108):
{
	String date = ((String) ((ArrayList<Object>)msg).get(1));
	byte[] bytes = ((byte[]) ((ArrayList<Object>)msg).get(2));
	int fileLength=((int) ((ArrayList<Object>)msg).get(3));
	answer = functions.saveReport(date, bytes, fileLength);
	try {
		client.sendToClient(answer);		
	}
	catch (Exception e)
	{
		e.getCause();
	}
	break;
}
case(109):
{
	String date = ((String) ((ArrayList<Object>)msg).get(1));
	answer = functions.getReport(date);
	try {
		client.sendToClient(answer);		
	}
	catch (Exception e)
	{
		e.getCause();
	}
	break;
}

 }
}

	  
 
  
    
  private int convertToScanCode(String code)
  {
	int scanCode=000;
	
	if (code.equalsIgnoreCase("Login"))
		scanCode=001;
	else
	if (code.equalsIgnoreCase("getInfo"))
		scanCode=002;
	else
	if (code.equalsIgnoreCase("Request"))
		scanCode=003;
	else
	if (code.equalsIgnoreCase("getListOfSystems"))
		scanCode = 004;
	else 
	if (code.equalsIgnoreCase("SetExecutionDuration"))
		scanCode=005;
	else
	if(code.equalsIgnoreCase("getRequestHistory"))
		scanCode=007;
	else
	if(code.equalsIgnoreCase("trackRequest"))
		scanCode=8;
	else
	if(code.equalsIgnoreCase("getReq"))
		scanCode=9;
	else 
	if(code.equalsIgnoreCase("getlistOfRequests"))
		scanCode=102;
	else 
	if(code.equalsIgnoreCase("setTaskDuration"))
		scanCode=101;
	else
	if(code.equalsIgnoreCase("getlistOfExRequests"))
		scanCode=200;
	else
	if(code.equalsIgnoreCase("getReviewAndDecision"))
		scanCode=302;
	else 
	if(code.equalsIgnoreCase("setDiscussionLog"))
		scanCode=303;
	else
	if(code.equalsIgnoreCase("viewRequest"))
		scanCode=8;
	else
	if(code.equalsIgnoreCase("viewAssessment"))
		scanCode=304;
	else 
	if(code.equalsIgnoreCase("getListOfRequestsForReview"))
		scanCode = 305;
	else
	if (code.equalsIgnoreCase("insertDiscussionLog"))
		scanCode = 307;
	else
	if (code.equalsIgnoreCase("convertRidIDtoAssid"))
		scanCode = 308;
	else
	if(code.equalsIgnoreCase("getDeadlineByRequestID"))
		scanCode = 309;
	else
	if(code.equalsIgnoreCase("getAss"))
		scanCode = 310;
	else
	if(code.equalsIgnoreCase("enterNewExecutionRecord"))
		scanCode = 201 ;
	else
	if (code.equalsIgnoreCase("requestNewDeadline"))
		scanCode = 900;
	else
	if (code.equalsIgnoreCase("canceledNewDeadline"))
		scanCode = 901;
	else
	if (code.equalsIgnoreCase("denyRequest"))
		scanCode = 902;
	else
	if (code.equalsIgnoreCase("getUserName"))
		scanCode = 800;
	else
	if (code.equalsIgnoreCase("Pair"))
		scanCode = 801;
	else
	if (code.equalsIgnoreCase("approveRequest"))
		scanCode = 903;
	else
	if (code.equalsIgnoreCase("moreInformationIsRequired"))
		scanCode = 904;
	else
	if (code.equalsIgnoreCase("getExe"))
		scanCode = 802;
	else
	if(code.equalsIgnoreCase("setAssessment"))
		scanCode=103;
	else
	if (code.equalsIgnoreCase("trackRequest+"))
		scanCode = 202;
	else
	if (code.equalsIgnoreCase("getListOfRequestsForInspection"))
		scanCode = 905;
	else
	if (code.equalsIgnoreCase("ApproveEXDu"))
		scanCode =803;
	else
	if (code.equalsIgnoreCase("getUsersToAssign") )
		scanCode = 906;
	else
	if (code.equalsIgnoreCase("Duration") )
		scanCode = 804;
	else
	if (code.equalsIgnoreCase("GetExecutedEntity") )
		scanCode=203;
	else
	if (code.equalsIgnoreCase("insertNewExecutionReqForSave"))
		scanCode = 205;
	else
	if (code.equalsIgnoreCase("updateExecutedReq"))
		scanCode = 204;
	else
	if (code.equalsIgnoreCase("assignInspector"))
		scanCode=907;
	else
	if (code.equalsIgnoreCase("durationSet"))
		scanCode=805;
	else
	if (code.equalsIgnoreCase("getlistOfInspectionableRequests"))
		scanCode=908;
	else
	if (code.equalsIgnoreCase("getExecutionReport"))
		scanCode=909;
	else
	if (code.equalsIgnoreCase("getInspectionData"))
		scanCode=910;
	else
	if (code.equalsIgnoreCase("setInspectionData"))
		scanCode=911;
	else
	if (code.equalsIgnoreCase("getListOfEmployees"))
		scanCode=500;
	else
	if (code.equalsIgnoreCase("getExtend"))
		scanCode=806;
	else
	if (code.equalsIgnoreCase("getExtendinfo"))
		scanCode=807;
	else
	if (code.equalsIgnoreCase("switchChairman"))
		scanCode=501;
	else
	if (code.equalsIgnoreCase("approvDenyExt"))
		scanCode=808;
	else
	if (code.equalsIgnoreCase("NewAssessmentRow"))
		scanCode=104;	
	else
	if (code.equalsIgnoreCase("switchCommitteeMembers"))
		scanCode=502;
	else
	if (code.equalsIgnoreCase("getDeniedExecutions"))
		scanCode=206;
	else 
	if (code.equalsIgnoreCase("changeStatusAndDel"))
		scanCode=207;
	else
	if (code.equalsIgnoreCase("setUserAccess"))
		scanCode=503;
	else
	if(code.equalsIgnoreCase("changeStatus"))
		scanCode=105;
	else
	if(code.equalsIgnoreCase("updateFields"))
		scanCode=809;
	else
	if(code.equalsIgnoreCase("completeReq"))
		scanCode=810;
	else
		if(code.equalsIgnoreCase("complete"))
		scanCode=811;
	else
	if (code.equalsIgnoreCase("logOff"))
		scanCode=504;
	else
	if(code.equalsIgnoreCase("viewFile"))
		scanCode=106;
	else
	if(code.equalsIgnoreCase("getExecute"))
		scanCode=812;
	else
	if(code.equalsIgnoreCase("ridToUid"))
		scanCode=813;
	else
	if(code.equalsIgnoreCase("getListOfAllRequests"))
		scanCode=912;
	else
	if(code.equalsIgnoreCase("getListOfAllAssessments"))
		scanCode=913;
	else
	if(code.equalsIgnoreCase("getListOfAllExecutions"))
		scanCode=914;																							
	else
	if(code.equalsIgnoreCase("confirmRequest"))
		scanCode=915;	
    if(code.equalsIgnoreCase("getAllReq"))
        scanCode=814;
	else
	if(code.equalsIgnoreCase("suspend"))
		scanCode=815;  
	else
	if(code.equalsIgnoreCase("resume"))
		scanCode=816;
	else
	if(code.equalsIgnoreCase("susInfo"))
	    scanCode=817;     
	else
		if(code.equalsIgnoreCase("getActivityReport"))
			scanCode=107;
	else
		if(code.equalsIgnoreCase("setActivityReport"))
			scanCode=108;
	else
		if(code.equalsIgnoreCase("getReport"))
			scanCode=109;
	return scanCode;
	
}







/**
   * This method overrides the one in the superclass.  Called
   * when the server starts listening for connections.
   */
  protected void serverStarted()
  {
   // System.out.println
   //   ("Server listening for connections on port " + getPort());
  }
  
  /**
   * This method overrides the one in the superclass.  Called
   * when the server stops listening for connections.
   */
  protected void serverStopped()
  {
   // System.out.println
    //  ("Server has stopped listening for connections.");
  }
  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of 
   * the server instance (there is no UI in this phase).
   *
   * @param args[0] The port number to listen on.  Defaults to 5555 
   *          if no argument is entered.
   */
  public static void main(String[] args) 
  {
    int port = DEFAULT_PORT; //Port to listen on


    ServerGui serverGui = new ServerGui();
    serverGui.setVisible(true);
    serverGui.setSize(450, 300);
    
    
    //----------------------------------
    //   EchoServer sv = new EchoServer(port);
    
  }
}
//End of EchoServer class











